package com.sign2024.sign2024project.reporttest.controller;

import com.alibaba.excel.write.handler.AbstractRowWriteHandler;
import com.alibaba.excel.write.metadata.holder.WriteSheetHolder;
import com.alibaba.excel.write.metadata.holder.WriteTableHolder;
import com.sign2024.sign2024project.reporttest.ReportTest;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.util.CellRangeAddress;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

// 自定义写处理器，实现单元格合并
public class ReportTestWriteHandler extends AbstractRowWriteHandler {
    private final List<ReportTest> dataList;
    private final Map<String, Integer> mergeMap = new HashMap<>();
    private final Map<String, Integer> bigMergeMap = new HashMap<>();

    public ReportTestWriteHandler(List<ReportTest> dataList) {
        this.dataList = dataList;
        calculateMergeRanges();
    }

    int count = 0;

    @Override
    public void afterRowDispose(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder, Row row, Integer relativeRowIndex, Boolean isHead) {
        if (isHead) {
            return;
        }

        String mergeValue = dataList.get(relativeRowIndex).getMerge();
        if (mergeValue != null && mergeMap.containsKey(mergeValue)) {
            int firstRow = findFirstRowForMerge(mergeValue, dataList, "merge");
            int lastRow = firstRow + mergeMap.get(mergeValue) - 1;
            if (relativeRowIndex == lastRow - 1) {
                writeSheetHolder.getSheet().addMergedRegion(new CellRangeAddress(firstRow, lastRow, 3, 3));
            }
        }


        String bigMergeValue = dataList.get(relativeRowIndex).getBigMerge();
        if (bigMergeValue != null && bigMergeMap.containsKey(bigMergeValue)) {
            int firstRow = findFirstRowForMerge(bigMergeValue, dataList, "bigMerge");
            int rowsToMerge = bigMergeMap.get(bigMergeValue);
            int lastRow = firstRow + rowsToMerge - 1;

            if (relativeRowIndex == lastRow - 1) {
                writeSheetHolder.getSheet().addMergedRegion(new CellRangeAddress(firstRow, lastRow, 4, 4));
            }
        }
    }

    private void calculateMergeRanges() {
        for (int i = 0; i < dataList.size(); i++) {
            ReportTest item = dataList.get(i);
            if (item.getMerge() != null) {
                mergeMap.put(item.getMerge(), mergeMap.getOrDefault(item.getMerge(), 0) + 1);
            }
            if (item.getBigMerge() != null) {
                bigMergeMap.put(item.getBigMerge(), bigMergeMap.getOrDefault(item.getBigMerge(), 0) + 1);
            }
        }
    }

    private int findFirstRowForMerge(String value, List<ReportTest> dataList, String property) {
        for (int i = 0; i < dataList.size(); i++) {
            ReportTest item = dataList.get(i);
            if (property.equals("merge") && value.equals(item.getMerge())) {
                return i + 1; // +1 因为行号从0开始
            } else if (property.equals("bigMerge") && value.equals(item.getBigMerge())) {
                return i + 1;
            }
        }
        return -1;
    }
}